package poler.util
{
	import com.salesforce.objects.SingleEmailMessage;
	
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	import mx.controls.Tree;
	import mx.events.ValidationResultEvent;
	import mx.validators.DateValidator;
	import mx.validators.IValidatorListener;
	import mx.validators.NumberValidator;
	import mx.validators.StringValidator;
	import mx.validators.Validator;
	
	public class CustomValidator
	{
		private static var single:SingleEmailMessage = new SingleEmailMessage();
		private static const scMSG:String = "\n\nDo you want to send a report?";
		private var msg:String;
		private var reqF:Boolean;
		private var msgAr:Array;
        private var vResult:ValidationResultEvent;

        private var stV:StringValidator;
        private var nV:NumberValidator;
        private var dV:DateValidator;
        private var vV:Validator;
        private var arrObj:Array;
			
		public function CustomValidator()
		{
			msg = '';
			reqF = false;
			stV = new StringValidator();
			nV = new NumberValidator();
			dV = new DateValidator();
			vV = new Validator();
			arrObj = new Array();
			msgAr = new Array();
		}
		public function _vDate(dObj:Object,msg:String = null):void{
			if(dObj != null){
				arrObj.push(dObj);
				dV.source = dObj;
				dV.property = "selectedDate"
				vResult = dV.validate();
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _vDatagrid(dgObj:DataGrid,msg:String = null):void{
			if(dgObj != null){
				arrObj.push(dgObj);
				vV.listener = dgObj;
				vV.required = true;
				vResult = vV.validate(dgObj.dataProvider);
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _vTree(dgObj:Tree,msg:String = null):void{
			if(dgObj != null){
				arrObj.push(dgObj);
				vV.listener = dgObj;
				vV.required = true;
				vResult = vV.validate(dgObj.dataProvider);
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _vComboBox(cbObj:Object,msg:String = null):void{
			if(cbObj != null){
				arrObj.push(cbObj);
				nV.source = cbObj;
                nV.property = "selectedIndex";
                nV.minValue = 0;
                nV.required = true;
				vResult = nV.validate();
				trace('cb:'+vResult.type);
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _vNumericStepper(nsObj:Object,msg:String = null):void{
			if(nsObj != null){
				arrObj.push(nsObj);
				nV.source = nsObj;
                nV.property = "value";
                nV.lowerThanMinError = 'This field is required.';
                nV.required = true;
                nV.minValue = 1;
				vResult = nV.validate();
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _vTextField(Textobj:Object,msg:String = null):void{
			if(Textobj != null){
				arrObj.push(Textobj);
				stV.source = Textobj;
                stV.property = "text";
				vResult = stV.validate();
				if (vResult.type==ValidationResultEvent.INVALID){
				 	var cm:String = (msg != null) ? (msg) : ('');
				 	reqF = true;
				 	msgAr.push(cm);
				}
			}
		}
		public function _CustomMessageAlert(param:String,obj:Object = null,prop:* = null):void{
			//This will remove all the other messages from appearing
			if(obj != null){
				arrObj.push(obj);
				vV.listener = obj;
				vV.required = true;
				vV.validate(prop);
			}
			msgAr.splice(0,msgAr.length);
			msgAr.push(param);
		}
		public function _toggleEnableValidators():void{
			for(var f:int = 0; f < arrObj.length;f++){
				var tObject:Object = arrObj[f];
				  IValidatorListener(tObject).validationResultHandler(new ValidationResultEvent(ValidationResultEvent.VALID));
			}
		}
		//Custom Error 
		public function showWarnings():Boolean{
			if(msgAr.length > 0){
				var cmessage:String = '';
				for each(var obj:Object in msgAr){
					if(obj != ''){
						if(cmessage.length > 0) cmessage += '\n';
						cmessage += obj;
					}
				}
				if(cmessage.length > 0){
					Alert.show(cmessage,'Warning:');
					return false;
				}
				if(reqF){
					return false;
				}
			}
			return true;	
		}
//
	}
}
